#!/usr/bin/env bash

OS_NAME=`uname`

destination=${1}
version=${2}

if [ -z ${version} ]; then
    echo "Usage: ./build.sh [dest] [version]"
    echo "          [dest] must be: "
    echo "                       worm_agent"
    echo "                       worm_srv"
    exit
fi

if [ "$destination" = "worm_agent" ]; then
    bin_file="$destination"
    rm ${bin_file}
    rm ${bin_file}.${version}.${OS_NAME}.tar.gz
    go build -o ${bin_file} main.go

    cp conf/config_client.conf conf/config.conf
    cp toolbox/control_agent.sh toolbox/control.sh

    # 打包
    mkdir -p tmp/conf
    cp conf/config_client.conf tmp/conf/config.conf
    cp toolbox/control.sh tmp/control.sh
    cp ${bin_file} tmp/${bin_file}

    cd ./tmp
    tar -czf ${bin_file}.${version}.${OS_NAME}.tar.gz *
    cd ..
    cp ./tmp/${bin_file}.${version}.${OS_NAME}.tar.gz .

    rm -rf tmp

elif [ "$destination" = "worm_srv" ]; then
    bin_file="$destination"
    rm ${bin_file}
    rm ${bin_file}.${version}.${OS_NAME}.tar.gz
    go build -o ${bin_file} main.go

    cp conf/config_httpsrv.conf conf/config.conf
    cp toolbox/control_srv.sh toolbox/control.sh

    mkdir -p tmp/conf
    cp conf/config_httpsrv.conf tmp/conf/config.conf
    cp toolbox/control.sh tmp/control.sh
    cp ${bin_file} tmp/${bin_file}

    cd ./tmp
    tar -czf ${bin_file}.${version}.${OS_NAME}.tar.gz *
    cd ..
    cp ./tmp/${bin_file}.${version}.${OS_NAME}.tar.gz .

    rm -rf tmp

else
    echo "Usage: [dest] must be: "
    echo "                       worm_agent"
    echo "                       worm_srv"
fi
